package com.syni.mdd.yhd.common.all.controller.nopro;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.utils.RestTemplateUtils;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.service.BmsBusinessService;
import com.syni.mdd.yhd.common.all.service.BmsCommonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @className BmsCommonController
 * @description TOO
 * @Author cfx
 * @DATE 2019/5/29 9:31
 * @VERSION 1.0
 **/
@RestController
@RequestMapping("/proRpc")
public class BmsProRpcController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    BmsCommonService bmsCommonService;

    @Autowired
    BmsBusinessService bmsBusinessService;


    @PostMapping("/uploadImgFile")
    public String uploadImgFile(@RequestParam(name = "userAvatar") MultipartFile[] uploadFiles){
        logger.info("文件上传参数：" + uploadFiles[uploadFiles.length-1].getOriginalFilename());
        logger.info("文件格式：" + uploadFiles[uploadFiles.length-1].getContentType());
        Map<String,Object> uploadResultMap = bmsCommonService.uploadMoreImgFile(uploadFiles, Constants.COMMON_FLAG_TRUE_INT);
        logger.info("上传结果：" + uploadResultMap);
        if(!(uploadResultMap.containsKey("status") && ResultCode.CODE_NORMAL.equals(uploadResultMap.get("status")))){
            return "";
        }
        List<String> list = (List<String>) uploadResultMap.get("data");
        if(list.size() > 0) {
            return list.get(0);
        }
        return "";
    }

    @PostMapping("/test")
    public String test(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String params = "{\n" +
                "    \"nonce_str\":\"b4ddeb5804634758a8737b60d0c94b5a\",\n" +
                "    \"sign\":\"F7F6854BABA0CB94F0C32600782CA63982E2379DC0573389258FBBF100F878FC\",\n" +
                "    \"fee_type\":\"CNY\",\n" +
                "    \"mch_id\":\"1563829591\",\n" +
                "    \"cash_fee\":\"1\",\n" +
                "    \"total_fee\":\"1\",\n" +
                "    \"attach\":\"默认\",\n" +
                "    \"time_end\":\"20201010120534\",\n" +
                "    \"transaction_id\":\"4200000699202010107416488288\",\n" +
                "    \"bank_type\":\"OTHERS\",\n" +
                "    \"openid\":\"oBnK76tmmwnVHtLjjPi3SIRX5Ans\",\n" +
                "    \"out_trade_no\":\"764458561181843456\",\n" +
                "    \"appid\":\"wx4b0084c3c94f9abf\",\n" +
                "    \"trade_type\":\"APP\",\n" +
                "    \"result_code\":\"SUCCESS\",\n" +
                "    \"is_subscribe\":\"N\",\n" +
                "    \"return_code\":\"SUCCESS\"\n" +
                "}";
        Map<String,String> po = JSON.parseObject(params,Map.class);
        String externalURL = "http://dg.syni.com/MDD_API/commonTool/common/forwardingInterface";
        //params.put("notifyUrl",notifyUrl);
        RestTemplateUtils.forwardPost(externalURL,request,po);
        return "SUCCESS";
    }
}
